package drawArrow;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: code-snippet
 * @className: JfreeChart
 * @description:
 * @author: JLONG
 * @date: 2024/8/21 17:40
 * @version: 1.0
 */
public class JfreeChart {
    private static final float STROKE_WIDTH = 3.0f; // 线宽，单位像素
//    private static final int ARROW_HEIGHT = 40; // 箭头高度，单位像素
//    private static final int ARROW_LENGTH = 20; // 箭头宽度，单位像素

    private static Line2D getLine(double x1, double y1, double x2, double y2, int width, int height) {
        return new Line2D.Double(x1 * width, y1 * height, x2 * width, y2 * height);
    }

    public static void drawArrow(File file) {
        File imageWithArrow = new File(file.getParentFile(), "ARROW.png");
        BufferedImage src = null;
        try {
            src = ImageIO.read(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 创建画笔
        Graphics2D g2d = src.createGraphics();

        int width = src.getWidth();
        int height = src.getHeight();
        Stroke stroke = new BasicStroke(
                STROKE_WIDTH,
                BasicStroke.CAP_SQUARE,
                BasicStroke.JOIN_BEVEL,
                15.0f,
                null,
                5.0f);
        g2d.setStroke(stroke);
        g2d.setColor(new Color(0, 0, 0));

        List<CoordinateDto> list = new ArrayList<>();

        list.add(new CoordinateDto(0.300, 0.500));
        list.add(new CoordinateDto(0.500, 0.300));
        list.add(new CoordinateDto(0.700, 0.500));
        list.add(new CoordinateDto(0.600, 0.500));
        list.add(new CoordinateDto(0.600, 0.800));
        list.add(new CoordinateDto(0.400, 0.800));
        list.add(new CoordinateDto(0.400, 0.500));
        list.add(new CoordinateDto(0.300, 0.500));

        for (int i = 0; i < list.size() - 1; i++) {
            g2d.draw(getLine(list.get(i).getX(), list.get(i).getY(), list.get(i + 1).getX(), list.get(i + 1).getY(), width, height));
        }

        g2d.dispose();
        try {
            ImageIO.write(src, "PNG", imageWithArrow);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
